from DBA.dbmanager import DBInterface

class Mesure:
	
	def __init__(self):
		self.id			= {"key":"ID","value":""}
		self.description	= {"key":"description","value":""}
		self.xposition		= {"key":"x","value":""}
		self.yposition		= {"key":"y","value":""}
		self.zposition		= {"key":"z","value":""}
		self.time		= {"key":"time","value":""}
		self.ensConfigWifi	= []
		self.ensListWifi	= []
		self.ensPerfWifi	= []
		self.__interfacebase	= DBInterface()
		self.id["value"]	= str(self.__interfacebase.createNewID());
	
	def addConfigWifi(self,config):
		self.ensConfigWifi.append(config)
	
	def nbConfigWifi(self):
		return self.ensConfigWifi.count()

	def addListWifi(self,List):
		self.ensListWifi.append(List)
	
	def nbListWifi(self):
		return self.ensListWifi.count()

	def addPerfWifi(self,perf):
		self.ensPerfWifi.append(perf)
	
	def nbPerf(self):
		return self.ensPerfWifi.count()

	def createByDico(self,dico):
		
		self.description["value"] 	= dico[self.description["key"]]
		self.xposition["value"] 	= dico[self.xposition["key"]]
		self.yposition["value"] 	= dico[self.yposition["key"]]
		self.zposition["value"] 	= dico[self.zposition["key"]]
		self.time["value"] 		= dico[self.time["key"]]

	def insertToDb(self,idCampagne):
		dico = self.createMesureDico(idCampagne)
		self.__interfacebase.insert(self.__interfacebase.mesuretablename,self.__interfacebase.mesuremapping,dico)
		for config in self.ensConfigWifi:
			config.insertToDb(self.id["value"])
		for liste in self.ensListWifi:
			liste.insertToDb(self.id["value"])
		for perf in self.ensPerfWifi:
			perf.insertToDb(self.id["value"])

	def updateIntoDb(self,idCampagne):
		dico = self.createMesureDico(idCampagne)
		self.__interfacebase.update(self.__interfacebase.mesuretablename,self.__interfacebase.mesuremapping,dico)
		for config in self.ensConfigWifi:
			config.updateIntoDb(self.id["value"])
		for liste in self.ensListWifi:
			liste.updateIntoDb(self.id["value"])
		for perf in self.ensPerfWifi:
			perf.updateIntoDb(self.id["value"])

	def createMesureDico(self,idCampagne):
		dico ={}
		dico[self.id["key"]]			= self.id["value"]
		dico["link_ca"]				= idCampagne
		
		dico[self.description["key"]]		= self.description["value"]
		dico[self.xposition["key"]]		= self.xposition["value"]
		dico[self.yposition["key"]]		= self.yposition["value"]
		dico[self.zposition["key"]]		= self.zposition["value"]
		dico[self.time["key"]]			= self.time["value"]
		return dico

if __name__=="__main__":
	bat = Batiment()
	bat.name["value"]		= "test";
	bat.description["value"]	= "Description du batiment";
	bat.xposition["value"]		= "34";
	bat.yposition["value"]		= "23";
	bat.zposition["value"]		= "15";
	bat.height["value"]		= "123";
	bat.width["value"]		= "23";
	
	from SalleModule import Salle
	sal = Salle()
	sal.name["value"]		= "salle 1";
	#sal.description["value"]	= "Description";
	sal.xposition["value"]		= "12";
	sal.yposition["value"]		= "12";
	sal.zposition["value"]		= "12";
	sal.height["value"]		= "432";
	sal.width["value"]		= "234";
	bat.addSalle(sal)
	bat.insertToDb();
